﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;
using TuanAnhPersonalWeb.Classes;
using MvcPaging;

namespace TuanAnhPersonalWeb.Areas.Admin.Controllers
{
     [Authorize(Roles = "admin,project")]
    public class ProjectController : Controller
    {
        private TuanAnhEntities db = new TuanAnhEntities();
        ProjectAccessConsolidator access = new ProjectAccessConsolidator();
        private const int defaultPageSize = 5;

        //
        // GET: /Admin/Project/
         
        public ActionResult Index(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            IList<ProjectModel> list = access.getAllProject().Where(e=>e.PTYPE==1).OrderByDescending(e => e.CREATEDATE).ToList();
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ListControl", list.ToPagedList(currentPageIndex, defaultPageSize));
            }
            else
            {
                return View(list.ToPagedList(currentPageIndex, defaultPageSize));
            }
        }
         
          public ActionResult Search(int? page, string keySearch = "code", string valSearch = "", string keyFilter = "1")
        {
            ViewData["keySearch"] = keySearch;
            ViewData["valSearch"] = valSearch;
            ViewData["keyFilter"] = keyFilter;
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            int filter = 1;
            switch (keyFilter)
            {
                case "1":
                    filter = 1;
                    break;
                case "2":
                    filter = 2;
                    break;
                case "3":
                    filter = 3;
                    break;
            }
            IList<ProjectModel> list = access.getAllProject().OrderByDescending(e => e.CREATEDATE).ToList();
            list = list.Where(e => e.PROJECT_NAME.ToLower().Contains(valSearch.ToLower()) && e.PTYPE==filter).ToList().ToPagedList(currentPageIndex, defaultPageSize);
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ListControl", list);
            }
            else
            {
                return View("Index", list);
            }

        }
        //
        // GET: /Admin/Project/Details/5
         
        public ViewResult Details(int id)
        {
            TUANANH__PROJECT tuananh__project = db.TUANANH__PROJECT.Single(t => t.PROJECT_ID == id);
            return View(tuananh__project);
        }

        //
        // GET: /Admin/Project/Create
         
        public ActionResult Create()
        {
            ViewData["Type"] = new SelectList(access.getAllPType(), "PTYPE", "PDESCRIPTION");
            return View();
        } 

        //
        // POST: /Admin/Project/Create
         
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(ProjectModel model)
        {
            if (ModelState.IsValid)
            {
                access.insertProject(model);
                return RedirectToAction("Index");  
            }
            ViewData["Type"] = new SelectList(access.getAllPType(), "PTYPE", "PDESCRIPTION");
            return View(model);
        }
        
        //
        // GET: /Admin/Project/Edit/5
          
        public ActionResult Edit(int id)
        {
          
            var data = access.getProjectbyID(id);
            ViewData["Type"] = new SelectList(access.getAllPType(), "PTYPE", "PDESCRIPTION",data.PTYPE);
            return View(data);
        }

        //
        // POST: /Admin/Project/Edit/5
          
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(ProjectModel item)
        {
            if (ModelState.IsValid)
            {

                access.updateProject(item);
                return RedirectToAction("Index");
            }
            ViewData["Type"] = new SelectList(access.getAllPType(), "PTYPE", "PDESCRIPTION", item.PTYPE);
            return View(item);
        }

        //
        // GET: /Admin/Project/Delete/5
         
        public ActionResult Delete(int id)
        {
            TUANANH__PROJECT tuananh__project = db.TUANANH__PROJECT.Single(t => t.PROJECT_ID == id);
            return View(tuananh__project);
        }

        //
        // POST: /Admin/Project/Delete/5
          
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            TUANANH__PROJECT tuananh__project = db.TUANANH__PROJECT.Single(t => t.PROJECT_ID == id);
            db.TUANANH__PROJECT.DeleteObject(tuananh__project);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}